//
//  ModifyLoginPasswordViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/11.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ModifyLoginPasswordViewController.h"

#import "AFNetworking.h"
@interface ModifyLoginPasswordViewController ()<UITextFieldDelegate>

@end

@implementation ModifyLoginPasswordViewController

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"LoginPWD_Edit" forKey:@"Action"];
    NSString *NSUTF8 = [NSString stringWithFormat:@"U_PassWord=%@&U_PassWord1=%@&U_PassWord2=%@",_modifyLoginPasswordTextField.text,_modifyLoginPasswordTextField2.text,_modifyLoginPasswordTextField3.text];
    NSUTF8 = [NSUTF8 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [parametersDic setValue:NSUTF8 forKey:@"Data"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        //    NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // 状态栏(statusbar)
//    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
//    
//    // 导航栏（navigationbar）
//    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    UILabel *modifyLoginPasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0+0+20, UISCREEN_WIDTH, 40)];
    modifyLoginPasswordLabel.text = @"      当前密码";
    modifyLoginPasswordLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:modifyLoginPasswordLabel];
    
    _modifyLoginPasswordTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(modifyLoginPasswordLabel.frame)+5, UISCREEN_WIDTH-120, 30)];
    _modifyLoginPasswordTextField.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _modifyLoginPasswordTextField.clearButtonMode = YES; //清除模式开启
    _modifyLoginPasswordTextField.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _modifyLoginPasswordTextField.delegate = self;
    [self.view addSubview:_modifyLoginPasswordTextField];

    
    UILabel *modifyLoginPasswordLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(modifyLoginPasswordLabel.frame)+1, UISCREEN_WIDTH, 40)];
    modifyLoginPasswordLabel2.text = @"      输入新密码";
    modifyLoginPasswordLabel2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:modifyLoginPasswordLabel2];
    
    _modifyLoginPasswordTextField2 = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(modifyLoginPasswordLabel2.frame)+5, UISCREEN_WIDTH-120, 30)];
    _modifyLoginPasswordTextField2.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _modifyLoginPasswordTextField2.clearButtonMode = YES; //清除模式开启
    _modifyLoginPasswordTextField2.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _modifyLoginPasswordTextField2.delegate = self;
    [self.view addSubview:_modifyLoginPasswordTextField2];

    
    UILabel *modifyLoginPasswordLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(modifyLoginPasswordLabel2.frame)+1, UISCREEN_WIDTH, 40)];
    modifyLoginPasswordLabel3.text = @"      确认新密码";
    modifyLoginPasswordLabel3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:modifyLoginPasswordLabel3];
    
    _modifyLoginPasswordTextField3 = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(modifyLoginPasswordLabel3.frame)+5, UISCREEN_WIDTH-120, 30)];
    _modifyLoginPasswordTextField3.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _modifyLoginPasswordTextField3.clearButtonMode = YES; //清除模式开启
    _modifyLoginPasswordTextField3.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _modifyLoginPasswordTextField3.delegate = self;
    [self.view addSubview:_modifyLoginPasswordTextField3];


    UIButton *modifyLoginPasswordButton = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(modifyLoginPasswordLabel3.frame)+20, UISCREEN_WIDTH-20-20, 40)];
    [modifyLoginPasswordButton setTitle:@"设置" forState:UIControlStateNormal];
    [modifyLoginPasswordButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    modifyLoginPasswordButton.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    [modifyLoginPasswordButton addTarget:self action:@selector(mflpbAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:modifyLoginPasswordButton];


}

// 设置按钮点击事件
- (void)mflpbAction:(UIButton *)sender {
    [self obtainData];
}

//UITextField控件协议方法：当点击返回按钮时方法执行
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
